﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using MpSoft.Library.BUSS.Categories;
using MpSoft.Library.DTO;
using MpSoft.Basic.Utils;

namespace MpSoft.Web.Controls.Modules
{
    public partial class NavigateControl : System.Web.UI.UserControl
    {
        #region Var

        private string CategorySeName = string.Empty;
        private string Title = string.Empty;

        #endregion

        #region Method

        protected string getNavigate()
        { 
            if (Request.QueryString["CategoryCode"] != null)
                CategorySeName = Request.QueryString["CategoryCode"].ToString();
            if (string.IsNullOrEmpty(CategorySeName) || CategorySeName.ToLowerInvariant().Equals("default")) return "";
            var obj = CategoryBUSS.Category_BySeName(CategorySeName);
            if (obj != null)
            {
                List<Category> listCate = new List<Category>(2);
                listCate.Add(obj);
                if (obj.ParentID != 0)
                    listCate = getListCate(obj.ParentID, listCate);
                StringBuilder sb = new StringBuilder();
                string url = "";
                sb.Append("<p class='navigate'>");
                sb.Append("<a href='/'>Trang chủ</a>");
                for (int j = listCate.Count - 1; j > -1; j--)
                {
                    obj = listCate[j];
                    url = SEOHelper.GetCategoryUrl(obj.SeName);
                    sb.Append("<a href='" + url + "'>" + obj.CategoryName + "</a>");
                }
                sb.Append("</p>");
                return sb.ToString();
            }
            return GetModuleOrther(CategorySeName);
        }

        private string GetModuleOrther(string SeName)
        {
            StringBuilder sb = new StringBuilder();
            switch (SeName)
            {
                case "tham-do-y-kien":
                    sb.Append("<p class='navigate'>");
                    sb.Append("<a href='/'>Trang chủ</a>");
                    sb.Append("<a href='" + SEOHelper.GetCategoryUrl(SeName) + "'>Thăm dò ý kiến</a>");
                    sb.Append("</p>");
                    break;
                case "tim-kiem-tin":
                    sb.Append("<p class='navigate'>");
                    sb.Append("<a href='/'>Trang chủ</a>");
                    sb.Append("<a href='" + SEOHelper.UrlSenameTitle(SeName, Title) + "'>Tìm kiếm tin</a>");
                    sb.Append("</p>");
                    break;
            }
            return sb.ToString();
        }

        private List<Category> getListCate(int CateParent, List<Category>  listCate)
        {
            var obj = CategoryBUSS.Category_GetByID(CateParent);
           
            while (obj != null)
            {
                listCate.Add(obj);
                obj = CategoryBUSS.Category_GetByID(obj.ParentID);
            }
            return listCate;
        }

        #endregion
        
        #region Event
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["CategoryCode"] != null)
                CategorySeName = Request.QueryString["CategoryCode"].ToString();
            if (Request.QueryString["Title"] != null)
                Title = Request.QueryString["Title"].ToString();
        }
        
        #endregion

        #region Properties
        #endregion
    }
}